package com.hatic.process;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.CodeCreator;
import org.jxstar.util.key.KeyCreator;

/**
 * 质量安检类
 * @author Thinkpad
 *
 */

public class ProjectQualityBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	/**
	 * 安全检查登记 表格提交时检查信息是否完整
	 * @param checkIds
	 * @return
	 */
	public String authCheck(String[] checkIds) {
		if (checkIds == null || checkIds.length == 0) {
			setMessage("没有选择登记单！");
			return _returnFaild;
		}
		
		for (int i = 0, n = checkIds.length; i < n; i++) {
			Map<String,String> umpPlan = queryCheck(checkIds[i]);
			
			String recordResult = umpPlan.get("record_result");//检查结果
			String recordMemo = umpPlan.get("record_memo");//检查结果描述

			if (recordResult.length() == 0 || recordMemo.length() == 0) {
				setMessage("信息不完全，不能提交！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	/**
	 * 问题整改通知单 表格提交时验证信息是否完整
	 * @param questIds
	 * @return
	 */
	public String authCheck1(String[] questIds) {
		if (questIds == null || questIds.length == 0) {
			setMessage("没有选择记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = questIds.length; i < n; i++) {
			Map<String,String> umpPlan = queryQuest(questIds[i]);
			
			String resultCause = umpPlan.get("result_cause");//不合格描述
			String replyDate = umpPlan.get("reply_date");//要求回复时间
			String endDate = umpPlan.get("end_date");//要求完成时间
			
			if (resultCause.length() == 0 || replyDate.length() == 0 || endDate.length() == 0 ) {
				setMessage("信息不完全，不能提交！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	/**
	 * 问题整改结果登记表格提交时验证信息是否完整
	 * @param questIds
	 * @return
	 */
	public String authCheck2(String[] questIds) {
		if (questIds == null || questIds.length == 0) {
			setMessage("没有选择记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = questIds.length; i < n; i++) {
			Map<String,String> umpPlan = queryQuest(questIds[i]);
			
			String questResult = umpPlan.get("quest_result");//整改处理结果
			
			if (questResult.length() == 0 ) {
				setMessage("信息不完全，不能提交！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 生成整改单
	 * @param checkIds
	 * @param auth_user
	 * @param auth_userid
	 * @return
	 */
	public String addQuest(String[] checkIds, String auth_user, String auth_userid){
		
						
		if (checkIds == null || checkIds.length == 0) {
			setMessage("没有选择记录！");
			return _returnFaild;
		}
		//判断：检查 结果是 通过，就不能生成整改单 
		for (int i = 0, n = checkIds.length; i < n; i++) {
			String checkId = checkIds[i];
			
			Map<String,String> mpPlan = queryCheck(checkIds[i]);
			
			String record_result = mpPlan.get("record_result");//检查结果
			String questCode = mpPlan.get("quest_code");//整改单号
			
			if(!(questCode.length() == 0) ){
				setMessage("不能再次生成整改单！");
				return _returnFaild;
			}
			
			if (record_result.equals("0")) {
				setMessage("检查结果是 通过，不能生成整改通知单！");
				return _returnFaild;
			}
			
			if (!insertQuest(checkId,auth_user,auth_userid)) {
				setMessage("创建整改单失败！");
				return _returnFaild;
			}
			
		}
				
		return _returnSuccess;
	}
	/**
	 * 生成问题整改单
	 * @param checkId
	 * @param auth_user
	 * @param auth_userid
	 * @return
	 */
	public boolean insertQuest(String checkId ,String auth_user , String auth_userid) {
		Map<String,String> mpPlan = queryCheck(checkId);
		
		//创建 整改单ID
		String  questId= KeyCreator.getInstance().createKey("quality_quest");
		//生成 整改单号
		String questCode = CodeCreator.getInstance().createCode("quality_quest2");
		String sql = "insert into quality_quest("+
					"project_id , project_name , project_code , project_user , project_userid,"+
					"auditing , dept_name , dept_id ,quest_id ,quest_code ,"+
					"quest_date ,quest_user , quest_userid,"+
					"check_type1 , plan_no,check_id)"+
					"values(?,?,?,?,?,"+
					"?,?,?,?,?,"+
					"?,?,?,"+
					"?,?,?)";
		DaoParam iparam = _dao.createParam(sql);
		iparam.addStringValue(mpPlan.get("project_id"));
		iparam.addStringValue(mpPlan.get("project_name"));
		iparam.addStringValue(mpPlan.get("project_code"));
		iparam.addStringValue(mpPlan.get("project_user"));
		iparam.addStringValue(mpPlan.get("project_userid"));
		iparam.addStringValue("0");
		iparam.addStringValue(mpPlan.get("dept_name"));
		iparam.addStringValue(mpPlan.get("dept_id"));
		//插入 问题整改单 ID，编号，和编制人 ，编制日期 --
		iparam.addStringValue(questId);
		iparam.addStringValue(questCode);
		iparam.addDateValue(DateUtil.getToday());
		iparam.addStringValue(auth_user);
		iparam.addStringValue(auth_userid);
		iparam.addStringValue(mpPlan.get("check_type1"));
		iparam.addStringValue(mpPlan.get("plan_no"));
		iparam.addStringValue(mpPlan.get("check_id"));
		
		if (!_dao.update(iparam)) {
			_log.showError("生成失败！");
			return false;
		}
		
		//更新 检查登记里面的 整改编号
		String usql = "update quality_check set quest_code = ? , quest_id = ?  where check_id = ?";
		DaoParam uparam = _dao.createParam(usql);
		uparam.addStringValue(questCode);
		uparam.addStringValue(questId);
		uparam.addStringValue(checkId);
		if (!_dao.update(uparam)) {
			_log.showError("更新检查登记里面的整改编号出错！");
			return false;
		}
		
		return true;
	}
	/**
	 * 查找 检查登记表里面的 信息 
	 * @param checkId
	 * @return
	 */
	public Map<String,String> queryCheck(String checkId) {
		String sql = "select * from quality_check where check_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(checkId);
		return _dao.queryMap(param);
	}
	/**
	 * 查找 整改单 信息
	 * @param questId
	 * @return
	 */
	public Map<String,String> queryQuest(String questId) {
		String sql = "select * from quality_quest where quest_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(questId);
		return _dao.queryMap(param);
	}
}
